<?php

class GaoDeAPI {

    private $key = 'b75c76129ebef2e4545c641d1cafa122';   //元祖高德

    /**
     * 地址转经纬度
     */
    public function getTrapezeAction($address){
        $address = urlencode($address);
        if(!$address){
           // $address = '上海市长宁区天山西路268号';
             return '';
        }
        if(strstr($address,'(')){
            $address = strstr($address,'(',true);
        }
        if(strstr($address,'（')){
            $address = strstr($address,'（',true);
        }
        $parameters = '&key='.$this->key.'&address='.$address;
        $url = 'https://restapi.amap.com/v3/geocode/geo?'.$parameters;
        $res = $this->getCurl($url);
        $res = json_decode($res,true);
        return $res;
    }

    /**
     * 行程规划 支持 步行 公交 自驾 骑行 货车 测距
     * 以下为自驾
     */
   public function GHAction(){
       $origin = '121.369838,31.217535';
       $destination = '116.4990234375,31.7129811694';
       $parameters = '&key='.$this->key.'&origin='.$origin.'&destination='.$destination;
        $url = 'https://restapi.amap.com/v3/direction/driving?'.$parameters;
        $res = $this->getCurl($url);
        $res = json_decode($res,true);
        echo '<pre>';
        var_dump($res);
    }


    /**
     * 返回静态地图       UNKNOWN_ERROR
     */
   public function staticMapAction(){
       $location = '116.49902,31.71298';
       $parameters = '&key='.$this->key.'&location='.$location;
        $url = 'https://restapi.amap.com/v3/staticmap?'.$parameters;
        $res = $this->getCurl($url);
        $res = json_decode($res,true);
        echo '<pre>';
        var_dump($res);
    }

    /**
     * 天气查询 需要在此之前获取城市的adcode
     */
    public function QueryWeatherAction(){
        $adcode = '341500';
        $parameters = '&key='.$this->key.'&city='.$adcode;
        $url = 'https://restapi.amap.com/v3/weather/weatherInfo?'.$parameters;
        $res = $this->getCurl($url);
        $res = json_decode($res,true);
        echo '<pre>';
        var_dump($res);
    }

    function getCurl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result =  curl_exec($ch);
        curl_close ($ch);
        return $result;

    }
}
